<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {pp_inputoptions} function plugin
 *
 * Type:     function<br>
 * Name:     pp_inputoptions<br>
 * Input:<br>
 *           - name       (optional) - string default "select"
 *           - values     (required if no options supplied) - array
 *           - options    (required if no values supplied) - associative array
 *           - selected   (optional) - string default not set
 *           - output     (required if not options supplied) - array
 * Purpose:  Prints the list of <option> tags generated from
 *           the passed parameters
 * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
 *      (Smarty online manual)
 * @author Monte Ohrt <monte at ohrt dot com>
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_pp_inputoptions($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
    
    $name = NULL;
	$list = NULL;
    $values = NULL;
    $options = NULL;
    $selected = array();
	$noselects = array();
	$selectable = array();
	$nosubs = array();
    $output = NULL;
    $style = NULL;
    $extra = '';
	$custom = '';
	$customp = '';
	
	//text
	$eid = NULL;
	$type = '';
	$from = NULL;
	$to = NULL;
	$fields = NULL;
	
	//
    foreach($params as $_key => $_val) {
        switch($_key) {
            case 'name':
                $$_key = (string)$_val;
                break;
            
			case 'list':
                $$_key = (string)$_val;
                break;
            
            case 'options':
				if( $_val == 'YES/NO' ) {
					$$_key = array('1' => 'Yes', '0' => 'No');
				} elseif( $_val == 'card_month' || $_val == 'card_year' ) {
					$custom = $_val;
				} elseif( $_val == 'order_status' ) {
					$custom = $_val;
				} elseif( substr($_val, 0, 1) == '[' && substr($_val, -1) == ']' ) {
					$bb = explode("|", substr($_val, 1, -1));
					$bc = array();
					foreach($bb as $k => $v) {
						if( strpos($v, "=") === false ) {
							$bc[] = $v;	
						} else {
							$vs = explode("=", $v);
							$bc[$vs['1']] = $vs['0'];
						}
					}
					$$_key = $bc;
				} else {
					$$_key = (array)$_val;
				}
                break;
                
            case 'values':
            case 'output':
				if( $_val == 'YES/NO' ) {
					$$_key = array('Yes', 'No');
				} elseif( substr($_val, 0, 1) == '[' && substr($_val, -1) == ']' ) {
					$bb = explode("|", substr($_val, 1, -1));
					foreach($bb as $k => $v) {
						$vs = explode("=", $v);
						$$_key[$vs['1']] = $vs['0'];
					}
				} else {
					$$_key = array_values((array)$_val);
				}
                break;
				
            case 'selected':
                $$_key = array_map('strval', array_values((array)$_val));
                break;
				
			case 'noselects':
				$$_key = (array)$_val;
				break;
				
			case 'selectable':
				$$_key = (array)$_val;
				break;
				
			case 'nosubs':
				$$_key = (array)$_val;
				break;
			
			case 'fields':
				$$_key = (string)$_val;
				break;

            case 'style':
                $$_key = (string)$_val;
                break;
			
			case 'custom':
			case 'customp':
				$$_key = (string)$_val;
				break;
           
		   case 'eid':
                $$_key = (string)$_val;
                break;
                
		   case 'type':
                $$_key = (string)$_val;
                break;
                
		   case 'from':
		   case 'to':
                $$_key = (int)$_val;
                break;
                
            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
                } else {
                    $smarty->trigger_error("pp_inputoptions: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
	
	//
	if( $custom == 'fromto' && isset($from) && isset($to) ) {
		for($i=$from; $i<=$to; $i++) {
			$options[$i] = $i;
		}
	} elseif( $custom == 'card_month' ) {
		$start = 1;
		$options = array();
		for($i=0; $i<12; $i++) {
			$options[$start + $i] = $start + $i;
		}
	} elseif( $custom == 'card_year' ) {
		$start = date("Y");
		$options = array();
		for($i=0; $i<15; $i++) {
			$options[$start + $i] = $start + $i;
		}
	} elseif( $custom == 'countries' ) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'country.class.php');
		$countryObj = new Country();
		$countries = $countryObj->getCountriesFormat();
		
		$options = $countries;
		if( !isset($selected) || sizeof($selected) == 0 || $selected == 0 ) {
			$selected = array(223);
		}
	} elseif( $custom == 'country_zones' ) {
		if( $customp > 0 ) {
			$countries_id = $customp;
		} else {
			$countries_id = 223;
		}
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'zone.class.php');
		$zoneObj = new Zone();
		$zones = $zoneObj->getZonesFormat($countries_id);
		
		$options = $zones;
	} elseif( $custom == 'order_status' ) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'order.status.class.php');
		$osObj = new OrderStatusBase();
		$options = $osObj->getItemsFormat();
		$style = 'Default';
	}
	
    if (!isset($options) && !isset($values))
        return ''; /* raise error here? */

    $_html_result = '';
	if( isset($_REQUEST[$name]) ) {
		$selected = (array)$_REQUEST[$name];
	}
	
	//
	if( $type == 'text' ) {
		if (isset($options)) {
			foreach ($options as $_key=>$_val) {
				if( in_array($_key, $selected) ) {
					return $_val;
				}
			}
		}
		if( isset($style) ) {
			return $style;	
		}
		return '';
	}
	
	//
    if (isset($options)) {
		if( isset($fields) ) {
			$fields = explode(',',$fields);
			if( sizeof($fields) == 2 ) {
				$opts = array();
				foreach($options as $k => $v) {
					$opts[$v[$fields]['0']] = $v[$fields]['0'];
				}
				$options = $opts;
			}
		}
		
        if( isset($style) ) {
			if( $style == 'parent' ) {
				//$options = array_merge(array('0' => 'Top'), $options);
				$optionsb = array('0' => 'Top');
				foreach($options as $k => $v) {
					$optionsb[$k] = $v;
				}
				$options = $optionsb;
				unset($optionsb);
			} elseif( $style == 'category' ) {
				//$options = array_merge(array('0' => 'Default'), $options);
				$optionsb = array('0' => 'Top');
				foreach($options as $k => $v) {
					$optionsb[$k] = $v;
				}
				$options = $optionsb;
				unset($optionsb);
			} elseif( $style == 'blank' ) {
				//$options = array_merge(array('0' => 'Default'), $options);
				$optionsb = array('' => '');
				foreach($options as $k => $v) {
					$optionsb[$k] = $v;
				}
				$options = $optionsb;
				unset($optionsb);
			} elseif( $style == 'text' ) {
				//$custom = stripslashes($custom);
				$options = explode('\n', $custom);
				$optionsb = array('' => '');
				foreach($options as $k => $v) {
					if( strlen($v) > 0 ) {
						$optionsb[$v] = $v;
					}
				}
				$options = $optionsb;
				unset($optionsb);
			} else {
				$optionsb = array('' => $style);
				foreach($options as $k => $v) {
					$optionsb[$k] = $v;
				}
				$options = $optionsb;
				unset($optionsb);
			}
		}
		if( isset($nosubs) ) {
			$noselects = array_merge($noselects, $nosubs);
		}

        foreach ($options as $_key=>$_val)
            $_html_result .= smarty_function_pp_inputoptions_optoutput($_key, $_val, $selected, $noselects, $selectable);

    } else {
        
        foreach ($values as $_i=>$_key) {
            $_val = isset($output[$_i]) ? $output[$_i] : '';
            $_html_result .= smarty_function_pp_inputoptions_optoutput($_key, $_val, $selected, $noselects, $selectable);
        }
    }
	
	//
	if( isset($eid) ) {
		//if( isset($_REQUEST[$name]) ) {
		//	$value = (string)$_REQUEST[$name];
		//}
		if( strstr($name, '[]') === false ) {
			if( isset($_REQUEST[$name]) ) {
				$value = (string)$_REQUEST[$name];
			}
		} else {
			$nameb = str_replace('[]', '', $name);
			$name = str_replace('[]', '[a' . $eid . ']', $name);
			if( isset($_REQUEST[$nameb]['a' . $eid]) ) {
				$value = (string)$_REQUEST[$nameb]['a' . $eid];
			}
		}
	} else {
		//if( isset($_REQUEST[$name]) ) {
		//	$value = (string)$_REQUEST[$name];
		//}
		if( strstr($name, '[]') === false ) {
			if( isset($_REQUEST[$name]) ) {
				$value = (string)$_REQUEST[$name];
			}
		} else {
			$nameb = str_replace('[]', '', $name);
			if( !isset($_REQUEST[$nameb . '_index']) ) {
				$_REQUEST[$nameb . '_index'] = 0;
			}
			if( isset($_REQUEST[$nameb][$_REQUEST[$nameb . '_index']]) ) {
				$value = (string)$_REQUEST[$nameb][$_REQUEST[$nameb . '_index']];
			}
			$_REQUEST[$nameb . '_index']++;
		}
	}
	
	//
	if( isset($list) && $list == 1 ) {
		$name .= '[]';
	}

    if(!empty($name)) {
        $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
    }
	
    return $_html_result;
}

function smarty_function_pp_inputoptions_optoutput($key, $value, $selected, $noselects, $selectable) {
	if( !is_array($value) && in_array((string)$key, $noselects) ) {
		$_html_result = '<optgroup label="' . smarty_function_escape_special_chars($value) . '"></optgroup>' . "\n";
	} elseif(!is_array($value)) {
		if( sizeof($selectable) > 0 && !in_array((string)$key, $selectable) ) {
			$_html_result = '<optgroup label="' . smarty_function_escape_special_chars($value) . '"></optgroup>' . "\n";
		} else {
			$_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' .
				smarty_function_escape_special_chars($key) . '"';
			if (in_array((string)$key, $selected))
				$_html_result .= ' selected="selected"';
			$_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
		}
    } else {
        $_html_result = smarty_function_pp_inputoptions_optgroup($key, $value, $selected);
    }
    return $_html_result;
}

function smarty_function_pp_inputoptions_optgroup($key, $values, $selected) {
    $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
    foreach ($values as $key => $value) {
        $optgroup_html .= smarty_function_pp_inputoptions_optoutput($key, $value, $selected);
    }
    $optgroup_html .= "</optgroup>\n";
    return $optgroup_html;
}

/* vim: set expandtab: */

?>
